<?php
/**
 * (ajax)
 */
namespace App\Admin\Ajax\OverSeasBa;

use App\Models\Basics\SysICountry;
use App\Models\OverSeasBa\Warehouse;
use App\Models\OverSeasBa\WarehouseModel;
use Illuminate\Support\Facades\DB;
class WarehouseAjax {

    /**
     * 创建仓库
     * @param $param
     * @return \Illuminate\Http\JsonResponse
     */
    public function doCreate($param){

        try{

            if(!$param['plat_id'] || !$param['w_code'] || !$param['country_id'] || !isset($param['status']) || !$param['warehouse_model'] || !$param['warehouse_service']){
                return response()->json(['status' => false, 'message' => '参数缺失，请补全仓库信息！']);
            }
            $wCode = $param['w_code'];
            $w = Warehouse::query()->where('w_code',$wCode)->first();
            if($w) return response()->json(['status' => false, 'message' => $wCode.'已存在勿重复添加！']);

            $param['area_id'] = SysICountry::getAreaIdByCountryId($param['country_id']); //仓库站点
            $param['warehouse_range'] = [
                Warehouse::WAREHOUSE_REMOVALE,
                Warehouse::WAREHOUSE_DROPSHIPPING,
                Warehouse::WAREHOUSE_RETURN
            ];
            DB::beginTransaction();
            $flag = Warehouse::query()->create($param);

            if(!$flag){
                DB::rollBack();
                return response()->json(['status' => false, 'message' => '操作失败！']);
            }else{
                //仓库功能
                $warehouseModel = [];
                foreach ($param['warehouse_model'] as $key => $val){
                    $warehouseModel[$key]['warehouse_id'] = $flag->id;
                    $warehouseModel[$key]['model_id'] = $val;
                }
                $flag->warehouse_models()->createMany($warehouseModel);

                DB::commit();
                return response()->json(['status' => true, 'message' => '操作成功！']);
            }
        }catch (\Exception $e){
            DB::rollBack();
            report($e);
            return response()->json(['status' => false, 'message' => '系统异常！']);
        }
    }

    /**
     * 编辑仓库
     * @param $param
     * @return \Illuminate\Http\JsonResponse
     */
    public function doEdit($param){
        try{

            if(!$param['plat_id'] || !$param['w_code'] || !$param['country_id'] || !isset($param['status']) || !$param['warehouse_model'] || !$param['warehouse_service']){
                return response()->json(['status' => false, 'message' => '参数缺失，请补全仓库信息！']);
            }

            $id = $param['id'];
            $wCode = $param['w_code'];
            $ow = Warehouse::query()->where('id','<>',$id)->where('w_code',$wCode)->first();
            if($ow){
                return response()->json(['status' => false, 'message' => $wCode.'已存在勿重复编辑！']);
            }

            DB::beginTransaction();
            $w = Warehouse::query()->where('id',$id)->first();
            $w->w_code = $wCode;
            $w->plat_id = $param['plat_id'];
            $w->group_id = $param['group_id'];
            $w->area_id = SysICountry::getAreaIdByCountryId($param['country_id']);
            $w->in_code = $param['in_code'];
            $w->country_id = $param['country_id'];
            $w->status = $param['status'];
            $w->address = $param['address'];
            $w->postcode = $param['postcode'];
            $w->contact = $param['contact'];
            $w->phone = $param['phone'];
            $w->warehouse_model   = $param['warehouse_model'];
            $w->warehouse_service = $param['warehouse_service'];
            $w->warehouse_range   = $param['warehouse_range'] = [
                Warehouse::WAREHOUSE_REMOVALE,
                Warehouse::WAREHOUSE_DROPSHIPPING,
                Warehouse::WAREHOUSE_RETURN
            ];

            if(!$w->save()){
                DB::rollBack();
                return response()->json(['status' => false, 'message' => '操作失败！']);
            }else{
                //仓库功能
                WarehouseModel::query()->where('warehouse_id', $w->id)->delete(); //清空删除数据 （重置当前仓库功能）
                foreach ($param['warehouse_model'] as $key => $val){
                    WarehouseModel::query()->create([
                        'warehouse_id' => $w->id,
                        'model_id' => $val,
                    ]);
                }

                DB::commit();
                return response()->json(['status' => true, 'message' => '操作成功！']);
            }
        }catch (\Exception $e){
            DB::rollBack();
            report($e);
            return response()->json(['status' => false, 'message' => '系统异常！']);
        }
    }
}
